//===============================================================================
// Microsoft patterns & practices
// Web Client Software Factory
//-------------------------------------------------------------------------------
// Copyright (C) Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//-------------------------------------------------------------------------------
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Web.Configuration;
using System.Web;

namespace WCSFContrib.PageFlow.Utils
{
	/// <summary>
	/// Helper methods for working with HttpRequests
	/// </summary>
	public static class HttpRequestHelper
	{
		private static string[] _ExtensionsHandledByPageHandlerFactory = { ".aspx" };
		private const string LogoutRequestText = "$LoginStatusText$";
		private const string EventTargetText = "__EVENTTARGET";

		/// <summary>
		/// Determines if the handler can handle a page request.
		/// </summary>
		/// <param name="rawUrl">The URL of the requested page.</param>
		/// <returns>true if the page's extension is handled by the handler</returns>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings", MessageId = "0#")]
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1062:ValidateArgumentsOfPublicMethods", Justification = "Validation done by Guard class.")]
        public static bool IsHandledByPageHandlerFactory(string rawUrl)
		{
			Guard.ArgumentNotNullOrEmptyString(rawUrl, "rawUrl");

			rawUrl = rawUrl.ToLower(CultureInfo.CurrentCulture);
			foreach (string extension in _ExtensionsHandledByPageHandlerFactory)
			{
				if (rawUrl.Contains(extension.ToLower(CultureInfo.CurrentCulture)))
				{
					return true;
				}
			}
			return false;
		}		

		/// <summary>
		/// Gets or sets the extensions handled by the handler.
		/// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")]
		public static string[] ExtensionsHandledByPageHandlerFactory
		{
			get { return _ExtensionsHandledByPageHandlerFactory; }
			set { _ExtensionsHandledByPageHandlerFactory = value; }
		}

		/// <summary>
		/// Retrieves a list of extensions that the handler can 
		/// handle from an Http handler configuration section.
		/// </summary>
		/// <param name="httpHandlersSection">Configuration section to load.</param>
		public static void LoadExtensionsHandledByPageHandlerFactoryFromConfig(HttpHandlersSection httpHandlersSection)
		{
			List<string> validExtensions = new List<string>();

			foreach (HttpHandlerAction httpHandlerAction in httpHandlersSection.Handlers)
			{
				if (httpHandlerAction.Type == "System.Web.UI.PageHandlerFactory")
				{
					validExtensions.Add(httpHandlerAction.Path.Substring(httpHandlerAction.Path.LastIndexOf('.')));
				}
			}

			if (validExtensions.Count > 0)
			{
				_ExtensionsHandledByPageHandlerFactory = validExtensions.ToArray();
			}
		}
	}
}
